﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace EFong.Contract
{
    [DataContract]
    public class TouringParty : Order
    {
        public TouringParty()
        {
            OrderType = "自组团";
            CustomerStatus = "未收款";
            VendorStatus = "未付款";
        }

        private string _name = string.Empty;
        [DataMember]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private Customer _customer;
        [DataMember]
        public Customer Customer
        {
            get { return _customer; }
            set { _customer = value; }
        }

        private string _contactPerson = string.Empty;
        [DataMember]
        public string ContactPerson
        {
            get { return _contactPerson; }
            set { _contactPerson = value; }
        }

        private string _contactPersonTel = string.Empty;
        [DataMember]
        public string ContactPersonTel
        {
            get { return _contactPersonTel; }
            set { _contactPersonTel = value; }
        }

        private int _adultQty;
        [DataMember]
        public int AdultQty
        {
            get { return _adultQty; }
            set { _adultQty = value; }
        }

        private int _childQty;
        [DataMember]
        public int ChildQty
        {
            get { return _childQty; }
            set { _childQty = value; }
        }

        private bool _isShowOnWeb = true;
        [DataMember]
        public bool IsShowOnWeb
        {
            get { return _isShowOnWeb; }
            set { _isShowOnWeb = value; }
        }

        //业务员
        private User _salesMan;
        [DataMember]
        public User SalesMan
        {
            get { return _salesMan; }
            set { _salesMan = value; }
        }

        //计调员
        private User _provideTicketUser;
        [DataMember]
        public User ProvideTicketUser
        {
            get { return _provideTicketUser; }
            set { _provideTicketUser = value; }
        }

        //草拟中,正式使用
        private string _status = string.Empty;
        [DataMember]
        public string Status
        {
            get { return _status; }
            set { _status = value; }
        }

        private string _address = string.Empty;
        [DataMember]
        public string Address
        {
            get { return _address; }
            set { _address = value; }
        }

        public decimal CalcTotalPrice()
        {
            decimal result = 0;
            foreach (TouringPartyItem item in Items)
                result += item.TotalPrice;

            return result;
        }

        //private decimal _totalPrice;
        //[DataMember]
        public decimal TotalPrice
        {
            get
            {
                if (ChildQty == 0 && AdultQty == 0)
                {
                    decimal result = 0;
                    foreach (TouringPartyItem item in Items)
                        result += item.TotalPrice;

                    return result;

                }
                else
                    return AdultQty * ActAverageAdultPrice + ChildQty * ActAverageChildPrice;
            }
        }

        public decimal TotalVendorPrice
        {
            get
            {
                decimal result = 0;
                foreach (TouringPartyItem item in Items)
                    result += item.TotalVendorPrice;

                return result;
            }
        }

        public decimal AverageChildPrice
        {
            get
            {
                if (this.ChildQty == 0)
                    return 0;

                decimal result = 0;
                foreach (TouringPartyItem item in Items)
                {
                    switch (item.CalcType)
                    {
                        case "只计大人":
                            break;
                        case "只计儿童":
                            result += item.TotalPrice / this.ChildQty;
                            break;
                        case "大人和儿童":
                            result += item.TotalPrice / (this.ChildQty + this.AdultQty);
                            break;
                        default:
                            break;
                    }
                }

                return result;
            }
        }

        private decimal _actAverageChildPrice;
        [DataMember]
        public decimal ActAverageChildPrice
        {
            get { return _actAverageChildPrice; }
            set { _actAverageChildPrice = value; }
        }

        public decimal AverageChildVendorPrice
        {
            get
            {
                if (this.ChildQty == 0)
                    return 0;

                decimal result = 0;
                foreach (TouringPartyItem item in Items)
                {
                    switch (item.CalcType)
                    {
                        case "只计大人":
                            break;
                        case "只计儿童":
                            result += item.TotalVendorPrice / this.ChildQty;
                            break;
                        case "大人和儿童":
                            result += item.TotalVendorPrice / (this.ChildQty + this.AdultQty);
                            break;
                        default:
                            break;
                    }
                }

                return result;
            }
        }

        public decimal AverageAdultPrice
        {
            get
            {
                if (this.AdultQty == 0)
                    return 0;

                decimal result = 0;
                foreach (TouringPartyItem item in Items)
                {
                    switch (item.CalcType)
                    {
                        case "只计大人":
                            result += item.TotalPrice / this.AdultQty;
                            break;
                        case "只计儿童":
                            break;
                        case "大人和儿童":
                            result += item.TotalPrice / (this.ChildQty + this.AdultQty);
                            break;
                        default:
                            break;
                    }
                }

                return result;
            }
        }

        private decimal _actAverageAdultPrice;
        [DataMember]
        public decimal ActAverageAdultPrice
        {
            get { return _actAverageAdultPrice; }
            set { _actAverageAdultPrice = value; }
        }

        public decimal AverageAdultVendorPrice
        {
            get
            {
                if (this.AdultQty == 0)
                    return 0;

                decimal result = 0;
                foreach (TouringPartyItem item in Items)
                {
                    switch (item.CalcType)
                    {
                        case "只计大人":
                            result += item.TotalVendorPrice / this.AdultQty;
                            break;
                        case "只计儿童":
                            break;
                        case "大人和儿童":
                            result += item.TotalVendorPrice / (this.ChildQty + this.AdultQty);
                            break;
                        default:
                            break;
                    }
                }

                return result;
            }
        }

        public decimal TotalProfit
        {
            get
            {
                return TotalPrice - TotalVendorPrice;
                //decimal result = 0;
                //foreach (TouringPartyItem item in this.Items)
                //    result += item.Profit;

                //return result;
            }
        }

        private string _documentName;
        [DataMember]
        public string DocumentName
        {
            get { return _documentName; }
            set { _documentName = value; }
        }

        private byte[] _document;
        [DataMember]
        public byte[] Document
        {
            get { return _document; }
            set { _document = value; }
        }

        private string _remark;
        [DataMember]
        public string Remark
        {
            get { return _remark; }
            set { _remark = value; }
        }

        private List<TouringPartyItem> _items = new List<TouringPartyItem>();
        [DataMember]
        public List<TouringPartyItem> Items
        {
            get { return _items; }
            set { _items = value; }
        }
    }
}
